<?php

namespace App\Http\Controllers\Guest;

use App\Http\Controllers\Controller;
use App\Model\Catalogo;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\View;
use Illuminate\Http\Request;

class CatalogoController extends Controller
{
    //
    public function Index()
    {
        return view('catalogo.index');
    }

    public function search()
    {
        $catalogos = DB::table('catalogos')->where('category', 'like', '%' . request('string') . '%')->get();
        return view('catalogo.index', compact('catalogos'));
    }

    /**
     * Salva dados do Livro de Endereços
     * Informaçoes extraidas de: https://geti2p.net/pt-br/docs/naming
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Throwable
     */
    public function store(Request $request)
    {
        $request->validate([
            'name' => ['required', 'unique:catalogos', 'max:67'],
            'address' => ['required','min:516','max:616','unique:catalogos'],
        ]);
        $catalogo = new Catalogo();
        $catalogo->name = strtolower(request('name'));
        $catalogo->b64 = request('address');
        $catalogo->category = json_encode(explode(",", request('category')));
        $result = $catalogo->saveOrFail();
        return view('catalogo.index')->with('result', $result);
    }

    /**
     * Lista as rotas do Banco catalogo
     * @return \Illuminate\Http\Response
     */
    public function ListRoutes()
    {
        $catalogos = Catalogo::all();
        $contents = view::make('catalogo.hosts', compact('catalogos'));
        $response = Response::make($contents, 200);
        $response->header('Content-Type', 'text/plain');
        return $response;
    }
}
